<?php

require_once('../../../api/classes/class.mysqli_database.php');
require_once('../../../api/classes/class.taxamatch.php');
require_once('../../../api/classes/class.nearmatch.php');
require_once('../../../api/classes/class.normalize.php');

class install_authority {

	protected $query;
	protected $postfix;

	function __construct() {
	}

	function setName($name){
		$this->postfix = $name;
	}

	function getAuthorAbbr($data) {
		$data["authority"] = trim($data["authority"]);
		if( $data["authority"] != '') {
			$query = sprintf("INSERT INTO auth_abbrev_%s (`AUTH_ABBR`, `AUTH_FULL`) VALUES ('%s','%s');\r\n"
				, mysql_escape_string($this->postfix)
				, mysql_escape_string($data["authority"])
				, mysql_escape_string($data["abbr"])
			);
			$db->query($query);
		}
		return("");
	}

	function getSpecies($species_id, $species, $genus_id, $genus, $author = '') {
		$species_length = strlen($species);
		$norm = new Normalize();
		$nm = new NearMatch();
		$search_species_name = $norm->normalize( $species );
		$near_match_species = $nm->near_match( $species );
	
		$query = sprintf("INSERT INTO `splist_%s` (`SPECIES_ID`, `GENUS_ORIG`, `SPECIES`, `GENUS_ID`, `AUTHORITY`, `SP_LENGTH`, `NEAR_MATCH_SPECIES`, `SEARCH_SPECIES_NAME`) VALUES ('%s','%s','%s','%s','%s',%s,'%s','%s');\r\n"
			, mysql_escape_string($this->postfix)
			, mysql_escape_string($species_id)
			, mysql_escape_string($genus)
			, mysql_escape_string($species)
			, mysql_escape_string($genus_id)
			, mysql_escape_string($author)
			, mysql_escape_string($species_length)
			, mysql_escape_string($near_match_species)
			, mysql_escape_string($search_species_name)
		);
	
		return($query);
	}

	function getGenus($genus_id, $genus, $author = '') {
	
#		$authority_desc = $this->postfix;
		$genus_length = strlen($genus);
		$norm = new Normalize();
		$nm = new NearMatch();
		$search_genus_name = $norm->normalize( $genus );
		$near_match_genus = $nm->near_match( $genus );
	
		$query = sprintf("INSERT INTO `genlist_%s` (`GENUS_ID`, `GENUS`, `AUTHORITY`, `GEN_LENGTH`, `NEAR_MATCH_GENUS`, `SEARCH_GENUS_NAME`) VALUES  ('%s','%s','%s',%s,'%s','%s');\r\n"
			, mysql_escape_string($this->postfix)
			, mysql_escape_string($genus_id)
			, mysql_escape_string($genus)
			, mysql_escape_string($author)
			, mysql_escape_string($genus_length)
			, mysql_escape_string($near_match_genus)
			, mysql_escape_string($search_genus_name)
		);
	
		return($query);
	}

	function createTables() {
		$query = sprintf("CREATE TABLE IF NOT EXISTS `auth_abbrev_%s` (
			`AUTH_ABBR` varchar(200) NOT NULL,
			`AUTH_FULL` varchar(200) NOT NULL
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;\r\n", mysql_escape_string($this->postfix));
	
		$query .= sprintf("CREATE TABLE IF NOT EXISTS `genlist_%s` (
			`GENUS_ID` varchar(150) NOT NULL,
			`GENUS` varchar(50) default NULL,
			`AUTHORITY` varchar(150) default NULL,
			`GEN_LENGTH` int(2) default NULL,
			`NEAR_MATCH_GENUS` varchar(50) default NULL,
			`SEARCH_GENUS_NAME` varchar(50) default NULL,
			KEY `GENUS_ID` (`GENUS_ID`),
			KEY `GEN_LENGTH` (`GEN_LENGTH`),
			KEY `NEAR_MATCH_GENUS` (`NEAR_MATCH_GENUS`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;\r\n", mysql_escape_string($this->postfix));

		$query .= sprintf("CREATE TABLE IF NOT EXISTS `genus_id_matches_%s` (
			`genus_id` varchar(150) NOT NULL,
			`genus` varchar(50) default NULL,
			`genus_ed` int(11) default NULL,
			`phonetic_flag` char(1) default NULL,
			`sessId` varchar(100) default NULL
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;\r\n", mysql_escape_string($this->postfix));

		$query .= sprintf("CREATE TABLE IF NOT EXISTS `species_id_matches_%s` (
			`species_id` varchar(150) NOT NULL,
			`genus_species` varchar(50) default NULL,
			`genus_ed` int(11) default NULL,
			`species_ed` int(11) default NULL,
			`gen_sp_ed` int(11) default NULL,
			`phonetic_flag` char(1) default NULL,
			`sessId` varchar(100) default NULL
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;\r\n", mysql_escape_string($this->postfix));

		$query .= sprintf("CREATE TABLE IF NOT EXISTS `splist_%s` (
			`SPECIES_ID` varchar(150) NOT NULL,
			`GENUS_ORIG` varchar(50) default NULL,
			`SPECIES` varchar(60) default NULL,
			`GENUS_ID` varchar(150) default NULL,
			`AUTHORITY` varchar(150) default NULL,
			`SP_LENGTH` int(2) default NULL,
			`NEAR_MATCH_SPECIES` varchar(60) default NULL,
			`SEARCH_SPECIES_NAME` varchar(60) default NULL,
			 KEY `GENUS_ID` (`GENUS_ID`),
			 KEY `SP_LENGTH` (`SP_LENGTH`),
			 KEY `NEAR_MATCH_SPECIES` (`NEAR_MATCH_SPECIES`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;\r\n", mysql_escape_string($this->postfix));

		return($query);
	}

}
?>